package com.wgt.startx.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by wgt on 2017/7/30.
 */
public final class CodecUtil {
    private static final Logger logger = LoggerFactory.getLogger(CodecUtil.class);

    /**
     * 将URL 编码
     * @param source
     * @return
     */
    public static String encodeURL(String source){
        String target;
        try{
            target = URLEncoder.encode(source,"UTF-8");
        }catch (Exception e){
            logger.error("encode url failure",e);
            throw new RuntimeException(e);
        }
        return target;
    }



    /**
     * 将URl 解码
     * @param source
     * @return
     */
    public static String decodeURL(String source){
        String target;
        try {
            target = URLDecoder.decode(source, "UTF-8");
        }catch (Exception e){
            logger.error("decode url failure",e);
            throw new RuntimeException(e);
        }
        return target;
    }
}
